﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Estimate
{
    public partial class formProductWriteoffDetail : FormMDIChildTemplate
    {
        public formProductWriteoffDetail()
        {

        }
        InvoiceWriteoff invoiceWriteoff;

        public formProductWriteoffDetail(InvoiceWriteoff invoiceWriteoff)
        {
            InitializeComponent();
            this.invoiceWriteoff = invoiceWriteoff;
        }

        private void formMaterialProductWriteoffDetail_Load(object sender, EventArgs e)
        {
            Text = String.Format("Акт Списання №{0} з Складу {1} за {2}",
                invoiceWriteoff.InvoiceWriteoffNumber,
                invoiceWriteoff.StoreHouse.StoreHouseName,
                invoiceWriteoff.DateCreate.ToShortDateString());
            FillProduct();
            FillInvoiceRelocations();
            root.TranslateGrid(gridProduct);
            root.TranslateGrid(gridInvoice);
        }


        private void FillProduct()
        {
            var storeHouse = invoiceWriteoff.StoreHouse;
            IQueryable<Product> products = context.Products;

            if (textBoxProductName.Text.Length > 0)
                products = products.Where(x => x.ProductName.Contains(textBoxProductName.Text));
            if (textBoxProductCode.Text.Length > 0)
                products = products.Where(x => x.ProductCode.Contains(textBoxProductCode.Text));

            var productsInStoreHouse = products.Select
                (product => new ProductInStoreHouse
                {
                    ProductCategory = product.ProductCategory,
                    ProductCode = product.ProductCode,
                    ProductId = product.ProductID,
                    ProductName = product.ProductName,
                    Unit = product.Unit
                }).ToList();
            foreach (var productInStoreHouse in productsInStoreHouse)
            {
                var cntIn =
                    storeHouse.ProductHistories.Where(
                        x =>
                        x.StatusID == 20 && x.StoreHouse == storeHouse &&
                        x.ProductID == productInStoreHouse.ProductId).Sum(
                        x => x.Cnt);
                var cntOut =
                    storeHouse.ProductHistories.Where(
                        x =>
                        x.StatusID == 30 && x.StoreHouse == storeHouse &&
                        x.ProductID == productInStoreHouse.ProductId).Sum(
                        x => x.Cnt);

                var cnt = cntIn - cntOut;

                productInStoreHouse.Count = cnt;
            }
            productInStoreHouseBindingSource.DataSource = productsInStoreHouse.Where(x => x.Count > 0).ToArray();
        }

        private void buttonAdd_Click(object sender, EventArgs e)
        {
            decimal price = context.InvoiceIncomePrices.OrderByDescending(x => x.DateSet).First().Price;
            decimal count = 0;
            try { count = decimal.Parse(textBoxCount.Text); }
            catch { return; }

            if (productInStoreHouseBindingSource.Current != null)
            {
                ProductInStoreHouse  viewproduct = (ProductInStoreHouse)productInStoreHouseBindingSource.Current;
                Product product = context.Products.Where(x => x.ProductID == viewproduct.ProductId).First();
                if (count > viewproduct.Count) return;

                ProductHistory historyWriteOff = new ProductHistory();
                historyWriteOff.Cnt = count;
                historyWriteOff.DateSet = invoiceWriteoff.DateCreate;
                historyWriteOff.Product = product;
                historyWriteOff.StatusID = 30;
                historyWriteOff.StoreHouse = invoiceWriteoff.StoreHouse;
                root.Submit(false);

                InvoiceWriteoffToHistory invoiceWriteoffToHistory = new InvoiceWriteoffToHistory();
                invoiceWriteoffToHistory.InvoiceWriteoff = invoiceWriteoff;
                invoiceWriteoffToHistory.ProductHistory = historyWriteOff;
                invoiceWriteoffToHistory.Count = count;
                invoiceWriteoffToHistory.Price = price * db.Dollar;
                invoiceWriteoffToHistory.PriceTotal = decimal.Round(price * db.Dollar * count, 3);
                invoiceWriteoffToHistory.InvoiceRelocationToHistoryID = null;

                root.Submit(false);

                FillInvoiceRelocations();
                FillProduct();

            }
        }
        private void FillInvoiceRelocations()
        {
            var invoice = from invoiceWriteOffToHistory in context.InvoiceWriteoffToHistories
                          from histories in context.ProductHistories
                          where invoiceWriteOffToHistory.ProductHistory == histories && invoiceWriteOffToHistory.InvoiceWriteoff == invoiceWriteoff
                          select new ProductInInvoice
                          {

                              Count = invoiceWriteOffToHistory.Count,
                              InvoiceToHistoryID = invoiceWriteOffToHistory.InvoiceWriteoffToHistoryID,
                              Price = invoiceWriteOffToHistory.Price,
                              PriceTotal = invoiceWriteOffToHistory.PriceTotal,
                              ProductName = histories.Product.ProductName,
                              UnitName = histories.Product.Unit.UnitName
                          };
            productInInvoiceBindingSource.DataSource = invoice;
        }

        private void buttonRemove_Click(object sender, EventArgs e)
        {
            ProductInInvoice productInInvoice = (ProductInInvoice)productInInvoiceBindingSource.Current;
            if (productInInvoice != null)
            {
                db.DeleteInvoiceWriteoffToHistory(db.GetInvoiceWriteoffToHistory(productInInvoice.InvoiceToHistoryID));
                FillProduct();
                FillInvoiceRelocations();
            }
        }

        private void textBoxProductCode_TextChanged(object sender, EventArgs e)
        {
            FillProduct();
        }

        private void textBoxProductName_TextChanged(object sender, EventArgs e)
        {
            FillProduct();
        }

        private void buttonPrint_Click(object sender, EventArgs e)
        {
            reportInvoiceWriteOff reportInvoiceWriteOff = new reportInvoiceWriteOff();

            reportInvoiceWriteOff.DataSource = productInInvoiceBindingSource;
            reportInvoiceWriteOff.xrLabelFirm.Lines = db.GetFirmOwn().GetFullDescription();
            
            reportInvoiceWriteOff.xrLabelHead1.Text = "Акт Списання № " + invoiceWriteoff.InvoiceWriteoffNumber;
            reportInvoiceWriteOff.xrLabelHead2.Text = "від " + invoiceWriteoff.DateCreate.ToShortDateString();
            reportInvoiceWriteOff.xrLabelStoreHouseTo.Text = invoiceWriteoff.StoreHouse.StoreHouseName;
            reportInvoiceWriteOff.xrLabelRemark.Text = invoiceWriteoff.Remark;
            reportInvoiceWriteOff.context = context;

            reportInvoiceWriteOff.ShowPreview();
        }

        private void textBoxProductCode_TextChanged_1(object sender, EventArgs e)
        {
            FillProduct();
        }

    }
}
